<%@ Page Title="Users" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<PersonalAlbumModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<span class="error"><%=Html.Encode(Model.ErrorMessage) %></span>
    <table>
        <tr>
            <td>
                <%=Model.Localize("Users.Name") %>
            </td>
            <td>
                <%=Model.Localize("Users.Email") %>
            </td>
            <td>
                <%=Model.Localize("Users.Roles") %>
            </td>
            <td>
                <%=Model.Localize("Users.Delete") %>
            </td>
            <td>
                <%=Model.Localize("Users.Edit") %>
            </td>
        </tr>
        <%foreach (var item in Model.Users)
          {%>
        <tr>
            <td id="name_<%=Html.Encode(item.Name) %>">
                <%=Html.Encode(item.Name) %>
            </td>
            <td id="email_<%=Html.Encode(item.Name) %>">
                <%=Html.Encode(item.Email) %>
            </td>
            <td id="roles_<%=Html.Encode(item.Name) %>">
                <%=Html.Encode(string.Join(",",item.Roles.ToArray())) %>
            </td>
            <td>
                <%using (Html.BeginForm<AdminController>(a => a.DeleteUser(item.Name)))
                  { %>
      <%--Html. AntiForgeryToken() --%>
                  
                <input type="submit" value="<%=Model.Localize("Users.Delete")%>" onclick="return confirm('<%=Model.Localize("Users.ConfirmDelete") %>');" />
                <%} %>
            </td>
            <td>
                <a href="javascript:void(0)" onclick="javascript:editUser('<%=Html.Encode(item.Name) %>')">
                    <%=Model.Localize("Users.Edit") %></a>
            </td>
        </tr>
        <%} %>
    </table>
    <input type="button" value="<%=Model.Localize("Users.Add") %>" onclick="addUser();" />
    <script type="text/javascript">
        $(document).ready(function() {
            $('#editUser').hide();
        });
        function addUser() {
            clearFields(); //clear fields
            document.getElementById('createNew').value = 'true';
            document.getElementById('Name').disabled = false;
            $('#editUser').slideDown();
        }
        function editUser(name) {
            clearFields(); //clear fields
            var username = document.getElementById('name_' + name).innerHTML;
            var email = document.getElementById('email_' + name).innerHTML;
            var roles = document.getElementById('roles_' + name).innerHTML.split(',');
            document.getElementById('Name').value = trim(username);
            document.getElementById('Email').value = trim(email);
            document.getElementById('Password').value = '      ';//just blank value
            var list = document.getElementById('Roles');
            for (var i = 0; i < roles.length; i++) {
                var r = trim(roles[i]);
                for (var j = 0; j < list.length; j++) {
                    var c = trim(list.options[j].value);
                    if (c == r) {
                        list.options[j].selected = true;
                        break;
                    }
                }
            }
            $('#editUser').slideDown();
        }
        function trim(value) {
            return value.replace(/^\s*/g, '').replace(/\s*$/g, '')
        }
        function cancelUserEdit() {
            clearFields();
            $('#editUser').slideUp();

        }
        function clearFields() {
            document.getElementById('createNew').value = 'false';
            var list = document.getElementById('Roles');
            document.getElementById('Password').value = '';
            document.getElementById('Name').value = '';
            document.getElementById('Name').disabled = true;
            document.getElementById('Email').value = '';
            for (var i = 0; i < list.length; i++) {
                list.options[i].selected = false;

            }
        }
    </script>

    <%using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { id = "userForm" }))
      { %>
      <%=Html.AntiForgeryToken()%>
    <fieldset id="editUser">
    <%=Html.Hidden("createNew", false)%>
        <table>
            <tr>
                <td>
                    <%=Model.Localize("Users.Name")%>
                </td>
                <td>
                    <%=Html.TextBox("Name", "", new { disabled = "disabled" })%>
                </td>
            </tr>
            <tr>
                <td>
                    <%=Model.Localize("Users.Email")%>
                </td>
                <td>
                    <%=Html.TextBox("Email")%>
                </td>
            </tr>
            <tr>
                <td>
                    <%=Model.Localize("Users.Roles")%>
                </td>
                <td>
                    <%=Html.ListBox("Roles")%>
                </td>
            </tr>
            <tr>
                <td>
                    <%=Model.Localize("Users.Password")%>
                </td>
                <td>
                    <%=Html.Password("Password")%>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="<%=Model.Localize("Users.Save") %>" onclick="document.getElementById('Name').disabled = false;" />
                </td>
                <td>
                    <input type="button" onclick="cancelUserEdit()" value="<%=Model.Localize("Users.Cancel") %>" />
                </td>
            </tr>
        </table>
    </fieldset>
    <%} %>
</asp:Content>
